//
//  NewsCommentCell.m
//  suRead
//
//  Created by 李 诗洋 on 15/11/13.
//  Copyright © 2015年 bao. All rights reserved.
//

#import "NewsCommentCell.h"
#import "M_Comment.h"
#import "FMDBHelpers.h"
#import "UIImageView+WebCache.h"

@interface NewsCommentCell()
@property (weak, nonatomic) IBOutlet UIImageView *zanIcon;
@property (weak, nonatomic) IBOutlet UIImageView *userPhoto;
@property (weak, nonatomic) IBOutlet UILabel *nameLable;
@property (weak, nonatomic) IBOutlet UILabel *commentContent;
@property (weak, nonatomic) IBOutlet UILabel *creatTimeLable;
@property (weak, nonatomic) IBOutlet UILabel *labelPraiseCount;
@property (weak, nonatomic) IBOutlet UILabel *labelCommentCount;
@property (weak, nonatomic) IBOutlet UIImageView *icon_comment;
@property (weak, nonatomic) IBOutlet UIButton *commentButton;

@property (strong,nonatomic) M_Comment *comment;
@end

@implementation NewsCommentCell

+ (CGFloat)heightWithContent:(NSString *)content_
{
    //个人信息高度 + 底部高度
    CGFloat height_ = 80.0f;
    
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    
    NSRange range = [content_ rangeOfString:@"</span>"];//判断字符串是否包含
    if (range.location !=NSNotFound)
    {
        NSString *jsString                 = [NSString stringWithFormat:@"<style type=\"text/css\"> \n"
                                              "span,pre{color: #9e9e9f;font-size: .8rem;}"
                                              "</style> \n"
                                              "%@ \n",content_];
        NSAttributedString * attrStr       = [[NSAttributedString alloc] initWithData:[jsString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        
        CGFloat contentHeight  = [attrStr boundingRectWithSize:CGSizeMake(screenWidth - 80.0f - 15.0f, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size.height;

        
        height_ += contentHeight;
    }
    else {
        
        CGFloat contentHeight = [content_ boundingRectWithSize:CGSizeMake(screenWidth - 80.0f - 15.0f, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]} context:nil].size.height;
        height_ += contentHeight;
    }

    return height_;
}

- (void)setCellData:(M_Comment *)cellData_
{
    self.comment = cellData_;
    
    self.userPhoto.layer.cornerRadius  = 20.0;//设置图片圆角的尺度
    self.userPhoto.layer.masksToBounds = YES;//没这句话它圆不起来
    [self.userPhoto sd_setImageWithURL:[NSURL URLWithString:cellData_.f_from_cover] placeholderImage:[UIImage imageNamed:@"icon_user"]];

    self.nameLable.text = cellData_.f_from_name;

    NSRange range = [cellData_.f_content rangeOfString:@"</span>"];//判断字符串是否包含
    if (range.location !=NSNotFound)
    {
        self.labelCommentCount.hidden = YES;
        self.icon_comment.hidden = YES;
        self.commentButton.hidden = YES;
        
        NSString *jsString                 = [NSString stringWithFormat:@"<style type=\"text/css\"> \n"
                                              "span,pre{color: #9e9e9f;font-size: .8rem;}"
                                              "</style> \n"
                                              "%@ \n",cellData_.f_content];
        NSAttributedString * attrStr       = [[NSAttributedString alloc] initWithData:[jsString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        
        self.commentContent.attributedText = attrStr;
        
    } else {
//        self.labelCommentCount.hidden = NO;
        self.icon_comment.hidden = NO;
        self.commentButton.hidden = NO;
        self.commentContent.text = cellData_.f_content;
    }

    self.creatTimeLable.text = cellData_.f_create_time;
}

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
}

- (IBAction)reply:(id)sender
{
    if (self.replyClickCallBack) {
        self.replyClickCallBack();
    }
}

@end
